//
//  PersonalInfo_ViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/9/5.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "PersonalInfo_ViewController.h"
#import "Masonry.h"
#import "BaseLabel.h"
#import "BaseTextField.h"
#import "BaseButton.h"
#import "BaseView.h"
#import "BaseTableView.h"
#import "DLWNetworkingParser.h"

@interface PersonalInfo_ViewController ()<UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong) UIImage *iamge;
@property (nonatomic, strong) UIImageView *imageview;
@property (nonatomic, strong) BaseLabel *lablePhone;
@property (nonatomic, strong) BaseTextField *textFieldNickName;
@property (nonatomic, strong) BaseTextField *textFieldTrueName;
@property (nonatomic, strong) BaseTextField *textFieldScale;
@property (nonatomic, strong) BaseTextField *textFieldPlace;
@property (nonatomic, strong) BaseTextField *textFieldReferrer;
@property (nonatomic, strong) BaseButton *buttonSave;
@property (nonatomic, strong) UIAlertController *alertController;
@property (nonatomic, strong) UIAlertController *alertControllerPic;
@property (nonatomic, strong) UIAlertController *alertControllerNick;
@end

@implementation PersonalInfo_ViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self handleData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"个人资料";
    
    [self createSubView];
    [self createAlertController];
}
- (void)handleData
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *ticket = [userDefault valueForKey:@"ticket"];
    NSString *uid = [userDefault valueForKey:@"uid"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:uid, @"id", ticket, @"ticket", nil];
    [DLWNetworkingParser POSTWithURL:@"http://api.danjiguanjia.com/v1/User/getUserInfo" par:dic success:^(id responseObject) {
        _textFieldTrueName.text = responseObject[@"data"][@"real_name"];
        _textFieldPlace.text = responseObject[@"data"][@"area"];
        _lablePhone.text = responseObject[@"data"][@"mobile"];
        _textFieldScale.text = responseObject[@"data"][@"account"];
        _textFieldReferrer.text = responseObject[@"data"][@"sale_phone"];
    } filed:^(NSError *error) {
        
    }];
}


#pragma mark - 创建子控件
- (void)createSubView
{
    // 读取 本地的图片
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSString *pathdata = [[pathArray firstObject] stringByAppendingPathComponent:@"iamgedate"];
    NSData *iamgeData = [NSData dataWithContentsOfFile:pathdata];
    _iamge = [UIImage imageWithData:iamgeData];
    if (_iamge == nil) {
        
        _imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"chatListCellHead"]];
    } else
    {
        _imageview = [[UIImageView alloc] initWithImage:_iamge];
    }
    [self.view addSubview:_imageview];
    [self addTapGestureRecognizer];
    
    _lablePhone = [[BaseLabel alloc] init];
    _lablePhone.text = @"18242360369";
    [self.view addSubview:_lablePhone];
    
    BaseLabel *lableNick = [[BaseLabel alloc] init];
    lableNick.text = @"昵  称";
    [self.view addSubview:lableNick];
    
    _textFieldNickName = [[BaseTextField alloc] init];
    _textFieldNickName.delegate = self;
    _textFieldNickName.placeholder = @"请输入昵称";
    _textFieldNickName.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textFieldNickName];
    
    BaseLabel *lableTureName = [[BaseLabel alloc] init];
    lableTureName.text = @"真实姓名";
    [self.view addSubview:lableTureName];
    
    _textFieldTrueName = [[BaseTextField alloc] init];
    _textFieldTrueName.delegate = self;
    _textFieldTrueName.placeholder = @"请输入您的真实姓名";
    _textFieldTrueName.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textFieldTrueName];
    
    BaseLabel *lableScale = [[BaseLabel alloc] init];
    lableScale.text = @"鸡场规模";
    [self.view addSubview:lableScale];
    
    _textFieldScale = [[BaseTextField alloc] init];
    _textFieldScale.delegate = self;
    _textFieldScale.placeholder = @"请选择鸡场规模";
    _textFieldScale.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textFieldScale];
    
    BaseLabel *lablePlace = [[BaseLabel alloc] init];
    lablePlace.text = @"鸡场地址";
    [self.view addSubview:lablePlace];
    
    _textFieldPlace = [[BaseTextField alloc] init];
    _textFieldPlace.delegate = self;
    _textFieldPlace.placeholder = @"请输入您的单位地址";
    _textFieldPlace.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textFieldPlace];

    BaseLabel *lableReferrer = [[BaseLabel alloc] init];
    lableReferrer.text = @"推荐人";
    [self.view addSubview:lableReferrer];
    
    _textFieldReferrer = [[BaseTextField alloc] init];
    _textFieldReferrer.delegate = self;
    _textFieldReferrer.placeholder = @"请输入推荐人手机号(选填)";
    _textFieldReferrer.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:_textFieldReferrer];
    
    _buttonSave = [BaseButton buttonWithType:UIButtonTypeSystem];
    [_buttonSave setTitle:@"保 存" forState:UIControlStateNormal];
    [_buttonSave setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _buttonSave.titleLabel.font = [UIFont systemFontOfSize:20];
    [_buttonSave setBackgroundColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]];
    [self.view addSubview:_buttonSave];
    [_buttonSave addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    // 约束控件
    // 头像
    [_imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).mas_offset(15);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(10);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.18);
        make.width.mas_equalTo(self.view.mas_height).multipliedBy(0.18);
    }];
    // 电话号
    [_lablePhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).mas_offset(15);
        make.left.mas_equalTo(_imageview.mas_right).mas_offset(10);
        make.height.mas_equalTo(_imageview.mas_height).multipliedBy(0.3);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.5);
    }];
   
    // lable昵称
    [lableNick mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageview.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(5);
        make.height.mas_equalTo(_imageview.mas_height).multipliedBy(0.4);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.2);
    }];
    
    [_textFieldNickName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageview.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(lableNick.mas_right).mas_offset(5);
        make.height.mas_equalTo(lableNick.mas_height);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-5);
    }];

    // 真实姓名
    [lableTureName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldNickName.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(5);
        make.height.mas_equalTo(_imageview.mas_height).multipliedBy(0.4);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.2);
    }];
    
    [_textFieldTrueName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldNickName.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(lableTureName.mas_right).mas_offset(5);
        make.height.mas_equalTo(lableTureName.mas_height);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-5);
    }];
  
    // 鸡场规模
    [lableScale mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldTrueName.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(5);
        make.height.mas_equalTo(_imageview.mas_height).multipliedBy(0.4);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.2);
    }];
    
    [_textFieldScale mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldTrueName.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(lableScale.mas_right).mas_offset(5);
        make.height.mas_equalTo(lableScale.mas_height);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-5);
    }];
   // 鸡场地址
    [lablePlace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldScale.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(5);
        make.height.mas_equalTo(_imageview.mas_height).multipliedBy(0.4);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.2);
    }];
    
    [_textFieldPlace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldScale.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(lablePlace.mas_right).mas_offset(5);
        make.height.mas_equalTo(lablePlace.mas_height);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-5);
    }];

    // 推荐人
    [lableReferrer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldPlace.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(5);
        make.height.mas_equalTo(_imageview.mas_height).multipliedBy(0.4);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.2);
    }];
    
    [_textFieldReferrer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldPlace.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(lableReferrer.mas_right).mas_offset(5);
        make.height.mas_equalTo(lableReferrer.mas_height);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-5);
    }];

    [_buttonSave mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldReferrer.mas_bottom).mas_offset(50);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(15);
        make.height.mas_equalTo(lableReferrer.mas_height);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-15);
    }];
    
}

#pragma mark - textField的协议方法
// 回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
// 开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == _textFieldNickName) {
        [self presentViewController:_alertControllerNick animated:YES completion:^{
            
        }];
    } else
    {
    [UIView animateWithDuration:0.2 animations:^{
        self.view.frame = CGRectMake(0, -60, self.view.frame.size.width, self.view.frame.size.height);
    }];
    }
}
// 结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.2 animations:^{
        self.view.frame = CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);
    }];
    
}

#pragma mark - imageview添加手势.获取本地的图片头像
- (void)addTapGestureRecognizer
{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction)];
    _imageview.userInteractionEnabled = YES;
    [_imageview addGestureRecognizer:tapGesture];
}
- (void)tapGestureAction
{
       [self presentViewController:_alertControllerPic animated:YES completion:^{
        
    }];
    
}
// pickerController的协议方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        _iamge = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        self.imageview.image = _iamge;
        // 将image写入本地
        NSData *iamgeData = UIImageJPEGRepresentation(_iamge, 0.1);
        NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
          NSString *pathdata = [[pathArray firstObject] stringByAppendingPathComponent:@"iamgedate"];
        BOOL isData = [iamgeData writeToFile:pathdata atomically:YES];
        if (isData) {
            NSLog(@"成功");
        } else {
            NSLog(@"失败");
        }
        
    }];

}

#pragma mark - button的点击事件
- (void)buttonAction:(BaseButton *)button
{
    // 存储数据
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *ticket = [userDefault valueForKey:@"ticket"];
    NSString *uid = [userDefault valueForKey:@"uid"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:uid, @"id", ticket,@"ticket",_textFieldTrueName.text,@"real_name", _textFieldPlace.text,@"area", _textFieldScale.text, @"account", _textFieldReferrer.text, @"sale_phone", nil];
    [DLWNetworkingParser POSTWithURL:@"http://api.danjiguanjia.com/v1/User/setUserInfo" par:dic success:^(id responseObject) {
        // 推出提示框
        _alertController.message = responseObject[@"msg"];
        [self presentViewController:_alertController animated:YES completion:^{
            
        }];
    } filed:^(NSError *error) {
        
    }];
}

#pragma mark - 创建alertcontroller
- (void)createAlertController
{
    // 修改信息时候的alertController
    _alertController = [UIAlertController alertControllerWithTitle:@"消息提醒" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [_alertController addAction:actionCancel];
    
    //获取头像的时候的提醒
    _alertControllerPic = [UIAlertController alertControllerWithTitle:@"获取头像方式" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"本地相册获取头像" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 推出本地的相册
        UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
        pickerController.delegate = self;
        pickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        [self presentViewController:pickerController animated:YES completion:^{
            
        }];
    }];
    
    UIAlertAction *actionTwo = [UIAlertAction actionWithTitle:@"拍照获取头像" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *actionThree = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [_alertControllerPic addAction:actionOne];
    [_alertControllerPic addAction:actionTwo];
    [_alertControllerPic addAction:actionThree];
    
    
    // 编辑昵称的时候弹出的alertController
#warning 出现约束的问题
    _alertControllerNick = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    [_alertControllerNick addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
       textField.placeholder = @"请输入昵称";
    }];
    
    UIAlertAction *actionQ = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        _textFieldNickName.text = _alertControllerNick.textFields.firstObject.text;
    }];
    UIAlertAction *actionC = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [_alertControllerNick addAction:actionC];
    [_alertControllerNick addAction:actionQ];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
